package com.tedu.charging.service.bill.rabbitmq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import com.tedu.charging.common.pojo.param.UpdateGunStatusParam;
import com.tedu.charging.service.bill.config.RabbitMQBillConfig;
import com.tedu.charging.service.bill.dao.BillFailMapper;
import com.tedu.charging.service.bill.dao.BillSuccesMapper;
import com.tedu.charging.service.bill.feignclient.DeviceClient;
import com.tedu.charging.service.bill.pojo.po.BillMQPO;
import com.tedu.charging.service.bill.pojo.po.ChargingBillFail;
import com.tedu.charging.service.bill.pojo.po.ChargingBillSucces;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class BillConsumer {

    @Autowired
    BillSuccesMapper billSuccesMapper;

    @Autowired
    BillFailMapper billFailMapper;

    @Autowired
    DeviceClient deviceClient;


    @RabbitListener(queues = RabbitMQBillConfig.DEAD_LETTER_QUEUE_NAME)
    public void handleBill(BillMQPO billMQPO, Message message, Channel channel) throws Throwable {
        System.out.println(billMQPO.toString());

        QueryWrapper queryWrapper = new QueryWrapper<>();
        String billId = billMQPO.getBillId();
        Integer userId = billMQPO.getUserId();
        Integer gunId = billMQPO.getGunId();

        queryWrapper.eq("bill_id", billId);
        queryWrapper.last("limit 1");

        //判断成功表中有没有订单号
        ChargingBillSucces chargingBillSucces = billSuccesMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(chargingBillSucces)) {
            //判断失败表有没有订单号
            ChargingBillFail chargingBillFail = billFailMapper.selectOne(queryWrapper);
            if (ObjectUtils.isEmpty(chargingBillFail)) {
                //没有订单号，添加数据
                ChargingBillFail insertPO = new ChargingBillFail();
                BeanUtils.copyProperties(billMQPO,insertPO);
//                insertPO.setBillId(billId);
//                insertPO.setUserId(userId);
//                insertPO.setGunId(gunId);
//                insertPO.setOperatorId(billMQPO.getOperatorId());
//                insertPO.setStationId(billMQPO.getStationId());
//                insertPO.setVehicleId(billMQPO.getVehicleId());
                insertPO.setFailDesc("设备没响应");
                billFailMapper.insert(insertPO);

            } else {
                //更新失败表
                ChargingBillFail updatePO = new ChargingBillFail();
//                updatePO.setUserId(userId);
//                updatePO.setGunId(gunId);
//                updatePO.setOperatorId(billMQPO.getOperatorId());
//                updatePO.setStationId(billMQPO.getStationId());
//                updatePO.setVehicleId(billMQPO.getVehicleId());
                BeanUtils.copyProperties(billMQPO,updatePO);
                billFailMapper.update(updatePO,queryWrapper);
            }
            //更改设备状态
            UpdateGunStatusParam updateGunStatusParam = new UpdateGunStatusParam();
            updateGunStatusParam.setId(gunId);
            updateGunStatusParam.setGunStatus(3);
            deviceClient.updateGunStatus(updateGunStatusParam);


        } else {
            //更新订单成功表
            chargingBillSucces.setUserId(userId);
            chargingBillSucces.setGunId(gunId);
            chargingBillSucces.setOperatorId(billMQPO.getOperatorId());
            chargingBillSucces.setStationId(billMQPO.getStationId());
            chargingBillSucces.setVehicleId(billMQPO.getVehicleId());

            billSuccesMapper.update(chargingBillSucces, queryWrapper);

        }

        long tag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(tag, true);


    }
}
